//
//  PickerDialogView.swift
//  UserApp_iOS
//
//  Created by linlin on 2019/10/12.
//  Copyright © 2019 linlin. All rights reserved.
//

import UIKit
import HandyJSON
// 子账户功能介绍
// 子账户是为方便一个企业多个人员同时使用APP的功能，可满足一个老板及多个采购员同时在线进行询价、交易等操作。具体功能如下：
// 每一个买家账户都是一个主账户
// 主账户可以开通一个或多个子账户
// 子账户同样具备查看联系商家，聊天询价、支付、收货、退换货及主账户授权的其他相关功能
// 主账户可以查看所有子账户相关信息
// 主账户可以删除子账户信息
let INTRO_STRING = "子账户功能介绍"
let INTRO_CONTENT_STRING = "子账户是为方便一个企业多个人员同时使用APP的功能，可满足一个老板及多个采购员同时在线进行询价、交易等操作。具体功能如下："
let DETAIL_ONT = "每一个买家账户都是一个主账户"
let DETAIL_TWO = "每一个买家账户都是一个主账户"
let DETAIL_THRELL = "每一个买家账户都是一个主账户"
let DETAIL_FOUE = "每一个买家账户都是一个主账户"
let DETAIL_FIVE = "每一个买家账户都是一个主账户"
let GAP = 10

class PickerDialogView: BaseDialogView, DialogDataSource {

    typealias Confirm = ((_ option1: Int, _ option2: Int, _ option3: Int)->Void)
    typealias Cancel = (()->Void)
    private var confirmHandler: Confirm?
    private var cancelHandler: Cancel?
    private var colums: Int = 0
    private var options1Items: [PickerViewData]?
    private var options2Items: [[PickerViewData]]?
    private var options3Items: [[[PickerViewData]]]?
    var option1: Int = 0
    var option2: Int = 0
    var option3: Int = 0

    init(confirmHandler: Confirm?, cancelHandler: Cancel?) {
        super.init(style: .alert)
        self.dataSource = self
        self.confirmHandler = confirmHandler
        self.cancelHandler = cancelHandler
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func dialogContentView() -> UIView {
        let width = screenWidth - 60
        let detail_width = width - 20 - 20
        
        let detailSize = self.getLabelSizeString(textStr: INTRO_CONTENT_STRING, size: CGSize.init(width: detail_width, height: 900), font: UIFont.systemFont(ofSize: 14))
        let list_width = detail_width - 30
        
        let detail_array = [DETAIL_ONT, DETAIL_TWO, DETAIL_THRELL, DETAIL_FOUE, DETAIL_FIVE]
        var height_array: [CGFloat] = []
        var height_sum: CGFloat = CGFloat(30 + 15 + 20 + GAP)  + detailSize.height
        for str in detail_array {
            let firstSize = self.getLabelSizeString(textStr: str, size: CGSize.init(width: list_width, height: 900), font: UIFont.systemFont(ofSize: 14))
            var height = firstSize.height
            if height < 20 {
                height = 20
            }
            height_array.append(height)
            height_sum += height
            height_sum += CGFloat(GAP)
        }
        // 白框加删除按钮
        let containerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: height_sum))
        // 中间白框
        let centerView: UIView = UIView(frame: CGRect(x: CGFloat(GAP), y: 30, width: width - 20, height: height_sum - 30))
        centerView.backgroundColor = .white
        centerView.layer.cornerRadius = 10
        centerView.layer.masksToBounds = true
        containerView.addSubview(centerView)
        // 删除按钮
        let cancelBtn: UIButton = UIButton(type: .custom)
        cancelBtn.frame = CGRect(x: width - 30, y: 0, width: 30, height: 30)
        cancelBtn.backgroundColor = .red
        cancelBtn.tag = 1
        cancelBtn.addTarget(self, action: #selector(buttonsAction(sender:)), for: .touchUpInside)
        containerView.addSubview(cancelBtn)
        
        let introLabel = UILabel.init(frame: CGRect(x: CGFloat(GAP), y: 15, width: detail_width, height: 20))
        introLabel.text = INTRO_STRING
        introLabel.textColor = colorTheme
        introLabel.font = .systemFont(ofSize: 18)
        introLabel.textAlignment = .center
        centerView.addSubview(introLabel)
        
        let detailLabel = UILabel.init(frame: CGRect(x: CGFloat(GAP), y: introLabel.bottom + CGFloat(GAP), width: detail_width, height: detailSize.height))
        detailLabel.text = INTRO_CONTENT_STRING
        detailLabel.textColor = .gray
        detailLabel.font = .systemFont(ofSize: 14)
        detailLabel.numberOfLines = 0
        detailLabel.lineBreakMode = .byWordWrapping
        detailLabel.textAlignment = .left
        centerView.addSubview(detailLabel)
        
        
        return containerView
    }
    
    /// 获取lable的大小
    /// - Parameters:
    ///   - textStr: label文本
    ///   - size: label最大size
    ///   - font: 字体
    /// - Returns: label的大小
    func getLabelSizeString(textStr: String, size: CGSize, font: UIFont) -> CGSize {
        let text: NSString = textStr as NSString
        let dic = [NSAttributedString.Key.font:font]
        let detailRect = text.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: dic, context: nil)
        let rectSize = detailRect.size
        return rectSize
    }
    @objc func buttonsAction(sender: UIButton) -> Void{
        if sender.tag == 1{
            cancelHandler?()
        } else {
            confirmHandler?(option1, option2, option3)
        }
        dismiss()
    }

}

protocol PickerViewData: HandyJSON {
    func getPickerViewText() -> String
}
